package org.iridium.swinggtalk.chat;

import java.awt.event.KeyEvent;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.iridium.swinggtalk.tools.MessageTools;
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ChatManager;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;

/**
 *
 * @author iridiumcao
 */
public class ChatDialog extends javax.swing.JDialog {

    private ChatManager chatManager;
    private String toUid;
    private Chat aChat;

    /** Creates new form ChatDialog */
    public ChatDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    public ChatDialog(java.awt.Frame parent, boolean modal, final String toUid, ChatManager chatManager) {
        this(parent, modal);
        this.setTitle(java.util.ResourceBundle.getBundle("org/iridium/swinggtalk/chat/resources/ChatDialog").getString("title.prefix") + toUid);
        this.toUid = toUid;
        this.chatManager = chatManager;

        aChat = chatManager.createChat(toUid,
                new MessageListener() {

                    public void processMessage(Chat chat, Message message) {
                        String upText = upTextArea.getText();
                        upTextArea.setText(upText + toUid + ": " + MessageTools.extractMessage(message.toXML()) + System.getProperty("line.separator"));
                    }
                });

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        splitPane = new javax.swing.JSplitPane();
        upPanel = new javax.swing.JPanel();
        upScrollPane = new javax.swing.JScrollPane();
        upTextArea = new javax.swing.JTextArea();
        chatPanel = new javax.swing.JPanel();
        chatScrollPane = new javax.swing.JScrollPane();
        chatTextArea = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        splitPane.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
        splitPane.setName("splitPane"); // NOI18N

        upPanel.setName("upPanel"); // NOI18N
        upPanel.setPreferredSize(new java.awt.Dimension(398, 220));

        upScrollPane.setName("upScrollPane"); // NOI18N

        upTextArea.setColumns(20);
        upTextArea.setEditable(false);
        upTextArea.setLineWrap(true);
        upTextArea.setRows(5);
        upTextArea.setWrapStyleWord(true);
        upTextArea.setName("upTextArea"); // NOI18N
        upScrollPane.setViewportView(upTextArea);

        javax.swing.GroupLayout upPanelLayout = new javax.swing.GroupLayout(upPanel);
        upPanel.setLayout(upPanelLayout);
        upPanelLayout.setHorizontalGroup(
            upPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(upScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 398, Short.MAX_VALUE)
        );
        upPanelLayout.setVerticalGroup(
            upPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(upScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 220, Short.MAX_VALUE)
        );

        splitPane.setTopComponent(upPanel);

        chatPanel.setName("chatPanel"); // NOI18N
        chatPanel.setPreferredSize(new java.awt.Dimension(398, 50));

        chatScrollPane.setName("chatScrollPane"); // NOI18N

        chatTextArea.setColumns(20);
        chatTextArea.setLineWrap(true);
        chatTextArea.setRows(5);
        chatTextArea.setWrapStyleWord(true);
        chatTextArea.setName("chatTextArea"); // NOI18N
        chatTextArea.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                chatTextAreaKeyReleased(evt);
            }
        });
        chatScrollPane.setViewportView(chatTextArea);

        javax.swing.GroupLayout chatPanelLayout = new javax.swing.GroupLayout(chatPanel);
        chatPanel.setLayout(chatPanelLayout);
        chatPanelLayout.setHorizontalGroup(
            chatPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(chatScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 398, Short.MAX_VALUE)
        );
        chatPanelLayout.setVerticalGroup(
            chatPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(chatScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 73, Short.MAX_VALUE)
        );

        splitPane.setRightComponent(chatPanel);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(splitPane, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(splitPane, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void chatTextAreaKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_chatTextAreaKeyReleased
        if (KeyEvent.VK_ENTER == evt.getKeyCode()) {
            String upText = upTextArea.getText();
            String chatText = chatTextArea.getText();

            try {
                aChat.sendMessage(chatText);
            } catch (XMPPException ex) {
                Logger.getLogger(ChatDialog.class.getName()).log(Level.SEVERE, null, ex);
            }
            upTextArea.setText(upText + java.util.ResourceBundle.getBundle("org/iridium/swinggtalk/chat/resources/ChatDialog").getString("chat.content.me") + chatText + System.getProperty("line.separator"));
            chatTextArea.setText("");
        }
    }//GEN-LAST:event_chatTextAreaKeyReleased
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel chatPanel;
    private javax.swing.JScrollPane chatScrollPane;
    private javax.swing.JTextArea chatTextArea;
    private javax.swing.JSplitPane splitPane;
    private javax.swing.JPanel upPanel;
    private javax.swing.JScrollPane upScrollPane;
    private javax.swing.JTextArea upTextArea;
    // End of variables declaration//GEN-END:variables
}
